package com.fb;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import java.io.File;

class LoadImageIcon extends Thread {
    int index;
    int size;
    boolean map;
    File file;
    FileBrowserPage fbp;
    LoadImageIcon(FileBrowserPage fbp,int index,int size,boolean map){
        this.fbp=fbp;
        this.index=index;
        this.size=size;
        this.map=map;
        if(map){
            int maped=fbp.willshowSorted[index];
            file=fbp.willshow[maped];
        }else file=fbp.willshow[index];
    }
    @Override
    public void run(){
        super.run();
        BitmapFactory.Options bfo=new BitmapFactory.Options();
        bfo.inJustDecodeBounds=true;
        BitmapFactory.decodeFile(file.getAbsolutePath(),bfo);
        if(bfo.mCancel||bfo.outWidth==-1||bfo.outHeight==-1){
            if(!interrupted()){
                fbp.ma.runOnUiThread(new Runnable(){
                        @Override 
                        public void run(){
                            if(map){
                                if(index<fbp.willshowSortedLen){
                                    int maped=fbp.willshowSorted[index];
                                    if(fbp.willshow[maped]==file){
                                        fbp.willshowIcons[maped]=fbp.typeImageBroken;
                                    }
                                }
                            }else if(index<fbp.willshowLen){
                                if(fbp.willshow[index]==file){
                                    fbp.willshowIcons[index]=fbp.typeImageBroken;
                                }
                            }
                            fbp.atIconLoaderEnd(LoadImageIcon.this,index);
                        }
                    });
            }
        }else{
            if(bfo.outWidth>=bfo.outHeight){
                bfo.outHeight=bfo.outWidth/size;
                bfo.outWidth=size;
            }else{
                bfo.outWidth=bfo.outHeight/size;
                bfo.outHeight=size;
            }
            bfo.inJustDecodeBounds=false;
            final Bitmap b=BitmapFactory.decodeFile(file.getAbsolutePath(),bfo);
            if(!interrupted()){
                fbp.ma.runOnUiThread(new Runnable(){
                        public void run(){
                            if(map){
                                if(index<fbp.willshowSortedLen){
                                    int maped=fbp.willshowSorted[index];
                                    if(fbp.willshow[maped]==file){
                                        fbp.willshowIcons[maped]=b;
                                    }
                                }
                            }else if(index<fbp.willshowLen){
                                if(fbp.willshow[index]==file){
                                    fbp.willshowIcons[index]=b;
                                }
                            }
                            fbp.atIconLoaderEnd(LoadImageIcon.this,index);
                        }
                    });
                fbp.invalidate();
            }
        }
    }
}
